package com.ftg.wymall.admin.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ftg.wymall.admin.dto.UserInfo;
import com.ftg.wymall.admin.entity.UmsAdmin;
import com.ftg.wymall.admin.mapper.UmsAdminMapper;
import com.ftg.wymall.admin.service.IUmsAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.wymall.admin.vo.UmsAdminVo;
import com.ftg.wymall.comm.tool.ConventUtil;
import com.ftg.wymall.comm.tool.UserPasswordCrypto;
import com.ftg.wymall.comm.vo.CodeStatusEnum;
import com.ftg.wymall.comm.vo.ConstUtil;
import com.ftg.wymall.comm.vo.RespMessUtil;
import com.ftg.wymall.feign.UploadAvatarFeign;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2023-10-20
 */
@Service
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements IUmsAdminService {

    /**
     * 用户登录方法
     *
     * @param userinfo 用户信息
     * @return 登录结果
     */
    @Override
    public RespMessUtil<UmsAdminVo> login(UserInfo userinfo) {

        QueryWrapper<UmsAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userinfo.getUsername());
        String secretPassword = UserPasswordCrypto.md5Password(userinfo.getPassword());
        queryWrapper.eq("password", secretPassword);
        UmsAdmin umsAdmin = this.getBaseMapper().selectOne(queryWrapper);

        if (Objects.isNull(umsAdmin)) {
            return RespMessUtil.build(CodeStatusEnum.INVALID_USERNAME_PASSWORD);
        } else {

            this.getBaseMapper().updateLoginTime(umsAdmin.getId());

            UmsAdminVo umsAdminVo = ConventUtil.conventSwapClass(umsAdmin, UmsAdminVo.class);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS, umsAdminVo);
        }
    }

    /**
     * 修改用户信息
     *
     * @param user 要修改的用户对象
     * @return 返回修改结果
     */
    @Override
    public RespMessUtil<Boolean> modifyUser(UmsAdmin user) {
        if (Objects.nonNull(user)) {
            // 密码加密
            if (Objects.nonNull(user.getPassword())) {
                user.setPassword(UserPasswordCrypto.md5Password(user.getPassword()));
            }
            return RespMessUtil.build(CodeStatusEnum.SUCCESS, this.getBaseMapper().modifyUmsAdminWithNonNull(user));
        }
        return RespMessUtil.build(CodeStatusEnum.FAILURE, false);
    }

    @Resource
    private UploadAvatarFeign uploadAvatarFeign;
    public static final String RESULT_AVATAR_URL = "url";

    /**
     * 上传头像
     *
     * @param id          用户id
     * @param avatarFile  头像文件
     * @return 上传结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public RespMessUtil<JSONObject> uploadAvatar(Long id, MultipartFile avatarFile) {

        if (Objects.nonNull(id) && Objects.nonNull(avatarFile)) {
            UmsAdmin umsAdmin = this.getBaseMapper().selectById(id);
            if (Objects.nonNull(umsAdmin)) {
                // 先删除原来的头像
                if(StrUtil.isNotEmpty(umsAdmin.getIcon())){
                    uploadAvatarFeign.delAvatar(umsAdmin.getIcon());
                }

                // 上传头像
                RespMessUtil<JSONObject> jsonObjectRespMessUtil = uploadAvatarFeign.uploadAvatar(avatarFile);
                System.out.println("==>"+jsonObjectRespMessUtil);
                if (jsonObjectRespMessUtil.getCode() == ConstUtil.STATE_VALUE) {
                    JSONObject data = jsonObjectRespMessUtil.getData();
                    // 更新数据头像
                    this.getBaseMapper().modifyAvatar(id, data.get(RESULT_AVATAR_URL).toString());
                    return jsonObjectRespMessUtil;
                }
            }
        }
        return RespMessUtil.build(CodeStatusEnum.FAILURE);
    }

}
